﻿
Imports osi.interface.storage

Public Class async_accessor_wrapper
    Inherits accessor_wrapper(Of sync_accessor_t)
    Implements accessor_t

    Public Sub New(ByVal a As sync_accessor_t)
        MyBase.New(a)
    End Sub

    Private Shared Function io(ByVal d As _do(Of Boolean)) As eventComb
        assert(Not d Is Nothing)
        Return New eventComb(Function() As Boolean
                                 Return d() AndAlso
                                        goto_end()
                             End Function)
    End Function

    Public Function read(ByVal key As String, ByVal result As pointer(Of bytes)) As eventComb _
                        Implements accessor_t.read
        Return io(Function() As Boolean
                      Dim r As bytes = Nothing
                      Return a.read(key, r) AndAlso
                             eva(result, r)
                  End Function)
    End Function

    Public Function write(ByVal key As String, ByVal value As bytes) As eventComb _
                         Implements accessor_t.write
        Return io(Function() As Boolean
                      Return a.write(key, value)
                  End Function)
    End Function

    Public Function [erase](ByVal key As String) As eventComb _
                           Implements accessor_t.erase
        Return io(Function() As Boolean
                      Return a.erase(key)
                  End Function)
    End Function

    Public Function exist(ByVal key As String, ByVal result As pointer(Of Boolean)) As eventComb _
                         Implements accessor_t.exist
        Return io(Function() As Boolean
                      Dim r As Boolean = False
                      Return a.exist(key, r) AndAlso
                             eva(result, r)
                  End Function)
    End Function

    Public Function sizeof(ByVal key As String, ByVal result As pointer(Of UInt64)) As eventComb _
                          Implements accessor_t.sizeof
        Return io(Function() As Boolean
                      Dim s As UInt64 = 0
                      Return a.sizeof(key, s) AndAlso
                             eva(result, s)
                  End Function)
    End Function
End Class
